package icegps.com.pingdj.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.media.session.MediaSession;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import icegps.com.pingdj.App;
import icegps.com.pingdj.R;
import icegps.com.pingdj.adapter.ContinueWorkAdapter;
import icegps.com.pingdj.bean.KeyValue;
import icegps.com.pingdj.bean.WorkBean;
import icegps.com.pingdj.constant.ActionKey;
import icegps.com.pingdj.constant.Token;
import icegps.com.pingdj.data.ble.WriteHelper;
import icegps.com.pingdj.utils.DateFormat;
import icegps.com.pingdj.utils.StringUtils;
import icegps.com.pingdj.utils.UnitUtils;
import icegps.com.pingdj.utils.Utils;

import static icegps.com.pingdj.constant.ActionKey.LAT;
import static icegps.com.pingdj.constant.ActionKey.LNG;
import static icegps.com.pingdj.constant.ActionKey.LOAD_TOKEN;
import static icegps.com.pingdj.constant.ActionKey.SAVE_TOKEN;
import static icegps.com.pingdj.constant.ActionKey.TITLE;

/**
 * Created by 111 on 2018/3/16.
 */

public class ContinueWorkActivity extends BaseActivity {
    ArrayList<KeyValue> continueWorkData = new ArrayList<>();
    @BindView(R.id.tv_empty)
    TextView tvEmpty;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_continue_work)
    TextView tvContinueWork;
    @BindView(R.id.ll_word_data)
    LinearLayout llWordData;
    double[] b = new double[3];

    @Override
    protected String setTitle() {
        return getString(R.string.continue_work);
    }

    @Override
    protected void init() {
        WorkBean w = App.getInstance().getDaoSession().getWorkBeanDao().load(Token.CONTINUE_WORK_TOKEN);
        String heightUnit = UnitUtils.index2Value(UnitUtils.HEIGHT, deviceStatus.getHeight());
        String lengthUnit = UnitUtils.index2Value(UnitUtils.LENGTH, deviceStatus.getLength());
        if (w != null) {
            int type = w.getType();
            switch (type) {
                case ActionKey.PLANE:
                    continueWorkData.add(new KeyValue(getString(R.string.do_plane), DateFormat.time2Date(w.getTime())));

                    continueWorkData.add(new KeyValue(getString(R.string.plane_set_height)
                            , UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), w.getStartSetHeight())
                            , heightUnit));

                    continueWorkData.add(new KeyValue(getString(R.string.offset), w.getOffset() + "cm"));

                    break;
                case ActionKey.SLOPE:

                    continueWorkData.add(new KeyValue(getString(R.string.do_slope), DateFormat.time2Date(w.getTime())));
                    //起点高度
                    continueWorkData.add(new KeyValue(getString(R.string.slope_start_height)
                            , UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), w.getStartSetHeight())
                            , heightUnit));
                    //终点高度
                    continueWorkData.add(new KeyValue(getString(R.string.slope_end_height)
                            , UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight()
                            , w.getEndLatitude()), heightUnit));
                    //起点终点距离
                    continueWorkData.add(new KeyValue(getString(R.string.start_end_distance)
                            , UnitUtils.conversion(UnitUtils.LENGTH, deviceStatus.getLength()
                            , w.getStartEndDistance()), lengthUnit));
                    //坡度
                    continueWorkData.add(new KeyValue(getString(R.string.gradient), StringUtils.setAccuracy(w.getGradient(), 3), "°"));
                    //斜率
                    continueWorkData.add(new KeyValue(getString(R.string.slope_percent), StringUtils.setAccuracy(w.getSlopePercent(), 3), "‰"));
                    //偏移
                    continueWorkData.add(new KeyValue(getString(R.string.offset), StringUtils.setAccuracy(w.getOffset(), 3), "cm"));
                    break;
            }
            llWordData.setVisibility(View.VISIBLE);
            tvEmpty.setVisibility(View.GONE);
            ContinueWorkAdapter continueWorkAdapter = new ContinueWorkAdapter(R.layout.item_continue_work, continueWorkData);
            // 判断Android当前的屏幕是横屏还是竖屏。横竖屏判断
            if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                //竖屏
                recyclerView.setLayoutManager(new LinearLayoutManager(activity));
            } else {
                //横屏
                recyclerView.setLayoutManager(new GridLayoutManager(activity, 2));
            }
            recyclerView.setAdapter(continueWorkAdapter);
        }

        tvContinueWork.setOnClickListener(v -> {
            if (status!=4){
                Utils.showToast(getString(R.string.location_status_do_no_user));
                return;
            }
            switch (w.getType()) {
                case ActionKey.PLANE: {
                    Intent intent = new Intent(activity, WorkMapActivity.class);
                    intent.putExtra(LNG, w.getStartLongitude());
                    intent.putExtra(LAT, w.getStartLatitude());
                    intent.putExtra(TITLE, getString(R.string.do_plane));
                    intent.putExtra(LOAD_TOKEN, Token.CONTINUE_WORK_TOKEN);
                    intent.putExtra(SAVE_TOKEN, Token.CONTINUE_WORK_TOKEN);
                    startActivity(intent);
                    finish();
                }
                break;
                case ActionKey.SLOPE: {
                    Intent intent = new Intent(activity, WorkMapActivity.class);
                    intent.putExtra(LNG, w.getStartLongitude());
                    intent.putExtra(LAT, w.getStartLatitude());
                    intent.putExtra(TITLE, getString(R.string.do_slope));
                    intent.putExtra(LOAD_TOKEN, Token.CONTINUE_WORK_TOKEN);
                    intent.putExtra(SAVE_TOKEN, Token.CONTINUE_WORK_TOKEN);
                    startActivity(intent);
                    finish();
                }
                break;
            }
            WriteHelper.write(WriteHelper.GRADER + w.toString());
        });
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_continue_work;
    }


}
